// 取数据里面对应的字段组成对象
export const assignParams = (data: any, part: any, searchParams: any = null) => {
  let para: any = {};
  if (data && Array.isArray(part.params) && part.params.length) {
    //接口参数
    part.params.forEach((item: any) => {
      for (const key in item) {
        para[key] = data[item[key]];
      }
    });

    //url参数
    if (part.state && part.state.params) {
      for (const key in part.state.params) {
        para[key] = part.state.params[key];
      }
    }
  }

  //扩展
  if (part.assign) {
    para = Object.assign(para, part.assign);
  } else if (part.state && part.state.assign) {
    para = Object.assign(para, part.state.assign);
  }

  //form参数
  if (searchParams) {
    if (part.formKey) {
      part.formKey.forEach((key: any) => {
        if (searchParams.value[key]) {
          para[key] = searchParams.value[key];
        }
      });
    } else if (part.state && part.state.formKey) {
      part.state.formKey.forEach((key: any) => {
        if (searchParams.value[key]) {
          para[key] = searchParams.value[key];
        }
      });
    }
  }
  return para;
};

// 取数据里面对应的字段组成对象
export const paramsData = (data: any, part: any, value:any =null) => {
  let params: any = {};
  data &&
    part.params &&
    part.params.forEach((item: any) => {
      for (const key in item) {
        if(item[key] === 'value' && value){
          params[key] = value;
        }else  if (key !== 'type') {
          params[key] = item.type && item.type === 'array' ? [showKeyName(data, item[key])] : showKeyName(data, item[key]);
        }
      }
    });
  if (part.state && part.state.assign) {
    params = Object.assign(params, part.state.assign);
  }
  return params;
};

//合并对象
export const mergeObjects = (obj1, obj2) => {
  for (const key in obj2) {
    if (obj2.hasOwnProperty(key)) {
      if (typeof obj2[key] === 'object' && obj2[key] !== null && !Array.isArray(obj2[key])) {
        if (typeof obj1[key] !== 'object' || obj1[key] === null || Array.isArray(obj1[key])) {
          obj1[key] = {};
        }
        mergeObjects(obj1[key], obj2[key]);
      } else {
        obj1[key] = obj2[key];
      }
    }
  }
};

//下载链接
export const downloadUrl = (url: string, name: any = '') => {
  const a = document.createElement('a');
  const body = document.querySelector('body');
  a.download = name;
  a.href = url;
  body!.append(a);
  a.click();
  window.URL.revokeObjectURL(a.href);
  a.remove();
};
//下载文件流
export const downloadFile = (data, fileName) => {
  // 利用response的响应内容构造一个Blob对象(通过Blob对象进行下载)
  const blob = new Blob([data]);

  // 创建一个隐藏的用来下载文件的a标签
  const elink = document.createElement('a');
  elink.download = fileName;
  elink.style.display = 'none';

  const src = URL.createObjectURL(blob);
  elink.href = src;
  document.body.appendChild(elink);

  // 模拟点击下载事件,进行下载
  elink.click();

  // 点击之后,移除我们定义的隐藏a标签
  document.body.removeChild(elink);

  // 移除文件对象
  URL.revokeObjectURL(src);
};

//取对应的key，包含.key
export const showKeyName = (data, key) => {
  if (!data) {
    return '';
  }
  if( key.includes('.') ){
    let keys:any = key.split('.');
    if( data[keys[0]] ){
      let str = keys.reduce((acc, curr) => acc[curr], data)
      return Array.isArray(str) ? str.join(',') : str;
    }else{
      return '';
    }
  }else{
    return Array.isArray(data[key]) ? data[key].join(',') : data[key] ;
  }
}

//从树形数据里面找到对应的数据
export const treeFindById = (tree,id,children = 'children') => {
  console.log('看下tree',tree,id,children);
  for (let i = 0; i < tree.length; i++) {
    if (tree[i].id === id) {
      return tree[i];
    }
    if (tree[i][children]) {
      const result = treeFindById(tree[i][children], id, children);
      if (result) {
        return result;
      }
    }
  }
  return null;
}

//给树形数据添加属性
export function addFullPath(tree,children='children',name="label",symbol='/',parentPath='') {  
  if (!tree || tree.length === 0) return null;  
  for (let i = 0; i < tree.length; i++) {  
    if( parentPath ){
      tree[i].fullPath = `${parentPath}${symbol}${tree[i][name]}`;  
    }else{
      tree[i].fullPath = `${tree[i][name]}`;  
    }
    if(tree[i][children]){
      addFullPath(tree[i][children], children, name, symbol, tree[i].fullPath)
    }
  }  
}  

